<template>
    <el-dialog :close-on-click-modal="false" :title="titleMap" v-model="visible" :width="1200" destroy-on-close @closed="$emit('closed')">
        <el-calendar ref="calendar" v-model="calendarDay" v-loading="loading">
            <template #dateCell="{ data }">
                <div class="el-calendar-day">
                    <div :class="{work: textContent(data.day) && textContent(data.day).isAllDay.value == 1 && textContent(data.day).type.value == 1, rest: textContent(data.day) && textContent(data.day).isAllDay.value == 1 && textContent(data.day).type.value == 2}" style="height: 100%;">
                        <div style="height: 35%;">
                            {{ data.day.split('-').slice(1)[1] }}
                            <span style="font-size: 1.1em;" v-if="data.isSelected">☕</span>
                        </div>
                        <div class="cell-day-box">
                            <template v-if="textContent(data.day)">
                                <div v-if="textContent(data.day).isAllDay.value == 1">{{ textContent(data.day).type.value == 1 ? '班' : '休'}}</div>
                                <div v-if="textContent(data.day).isAllDay.value == 0 && textContent(data.day).list.length > 0">
                                    <p style="font-size: 0.8em;" v-for="item in textContent(data.day).list">
                                        <template v-if="item.type.value == 2">
                                            <span>{{ item.startTime + '~' + item.endTime }}</span> 
                                            <a href="javascript:;" class="delete_link" @click="deleteSchedule(item)">删除休息时间<i class="el-icon-close"></i></a>
                                        </template>
                                    </p>
                                </div>
                            </template>
                            <template v-else>
                                <div>
                                    <el-button class="cell-check-button is-circle" icon="el-icon-check" @click="setSchedule(data.day, 1)">
                                    </el-button>
                                    <el-button class="cell-close-button is-circle" icon="el-icon-close" @click="setSchedule(data.day, 2)">
                                    </el-button>
                                </div>
                            </template>
                        </div>
                    </div>
                </div>
            </template>
        </el-calendar>
    </el-dialog>
</template>

<script>
import 'dayjs/locale/zh-cn';
export default {
    emits: ['success', 'closed'],
    data() {
        return {
            titleMap: '时间安排',
            loading: false,
            visible: false,
            teacherId: null,
            scheduleList: [],
            calendarDay: new Date(),
        }
    },
    mounted() {
       
    },
    methods: {
        //显示
        open(row) {
            this.teacherId = row.id;
            this.getScheduleList()
            this.visible = true;
            return this
        },
        getScheduleList() {
            let time = this.getDateFun()
            let params = {
                teacherId: this.teacherId,
                time: time
            }
            this.$API.teacher.schedule.list.get(params).then((res) => {
                this.scheduleList = res.data
            });
        },
        //删除时间
        deleteSchedule(row) {
            this.$confirm('确认是否要删除该休息时间？')
            .then((el) => {
                this.loading = true;
                this.$API.teacher.schedule.action.delete({id: row.id}).then((res) => {
                    this.loading = false;
                    if (res.error != 0) {
                        this.$notify.error(res.message);
                        return false;
                    }
                    this.getScheduleList()
                }).finally(() => {
                    this.loading = false;
                });
            }).catch((error) => {
            })
        },
        setSchedule(day, type) {
            let params = {
                teacherId: this.teacherId,
                startTime: day + ' 00:00:00',
                endTime: day + ' 23:59:59',
                type: type,
                isAllDay: 1,
            }

            this.loading = true;
            this.$API.teacher.schedule.action.post(params).then((res) => {
                this.loading = false;
                if (res.error != 0) {
                    this.$notify.error(res.message);
                    return false;
                }
                this.getScheduleList()
            }).finally(() => {
                this.loading = false;
            });
        },
        textContent(date) {
            let dayData = null;
			dayData = this.scheduleList.find((f) => f.date == date);
			return dayData;
        },
        getDateFun(time) {
            let now = time ? new Date(time) : new Date();
            let year = now.getFullYear(); //得到年份
            let month = now.getMonth(); //得到月份
            let date = now.getDate(); //得到日期
           
            month = month + 1;
            month = month.toString().padStart(2, "0");
            date = date.toString().padStart(2, "0");
            let defaultDate = `${year}-${month}-${date}`;
            return defaultDate;
        },
    }
}
</script>

<style lang="scss" scoped>

.el-calendar-table .el-calendar-day {
    box-sizing: border-box;
    padding: 8px;
    height: 85px;
}

.el-calendar-table .el-calendar-day:hover{
    cursor: pointer;
    background-color: unset !important;
}

.cell-day-box {
    height: 65%;
    display: flex;
    align-items: center;
    justify-content: center;
    text-align: justify;
}

.cell-check-button {
    color: white;
    background-color: rgb(23, 97, 72);
}

.cell-close-button {
    color: white;
    background-color: rgb(255, 202, 2);
    margin-left: 1em;
}

.el-button--small .is-circle {
    padding: 9px;
}

.el-button .is-circle {
    border-radius: 50%;
    padding: 12px;
}

.work {
    color: #fff;
    background-color: #b3e19d;
}

.rest {
    color: #fff;
    background-color: #fab6b6;
}

.is-selected {
  color: #1989fa;
}

.cell-day-box{
    .el-button:focus, .el-button:hover {
        color: white;
        border-color: var(--el-color-primary-light-7);
        // background-color: rgb(23, 97, 72);
        outline: 0;
    }   
} 


.delete_link {
    text-decoration: none;
    font-size: .8em;
    margin-left: 1em;
    color: red;
}

</style>
